<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\web\Controller;
use app\models\common\CallbackOrder;
use app\models\common\Good;
class CloudController extends Controller{
    //网站激活
    public function actionCheckAuth(){
         echo json_encode(['code'=>1,'msg'=>'网站已激活','type'=>1]);die;
        //echo json_encode(['code'=>2,'msg'=>'站点授权失败,请联系管理员处理!','type'=>2]);die;
        $request = Yii::$app->request;
        $userData=$_SESSION['userInfo'];
        if(Yii::$app->request->hostInfo!="https://bkycms.com"){
            if(!$userData){
                if(Yii::$app->params['isDev']==true){
                    echo json_encode(['code'=>1,'msg'=>'请先登录','data'=>Yii::$app->request->hostInfo,'type'=>3]);die;
                }else{
                    echo json_encode(['code'=>1,'msg'=>'请先登录','data'=>'/login','type'=>4]);die;
                }
            }
        }
        if ($request->isPost){
            $result=axios_request();
            $domainUrl=Yii::$app->request->hostInfo;
            $domainUrl = preg_replace("(^https?://)", "", $domainUrl);
            $data=array(
                'domain_url'=>$domainUrl,
                'auth_code'=>trim($result['auth_code']),
                'ip'=>gethostbyname($_SERVER['SERVER_NAME']),
                'auth_type'=>$result['auth_type']?:1,
            );
            $fileData=file_get_contents('./web/secret.json');
            $jsonData=json_decode(ybwmDecrypt($fileData,Yii::$app->params['channel_model_name']),true);
            //dd($jsonData);die;
            if($domainUrl=='bkycms.com' or $domainUrl=='v2.bkycms.com'){
                $domainUrl=$jsonData['domain_url'];
            }
            if($jsonData['authData']&&$jsonData['status']==1&&$jsonData['domain_url']==$domainUrl){
                $sessionInfo=Yii::$app->session->get('userInfo');
                if($sessionInfo['loginType']==2&&$sessionInfo['type']==3){
                    $authType=2;
                }else{
                    $authType=1;
                }
                echo json_encode(['code'=>1,'msg'=>'网站已激活','type'=>1,'authType'=>$authType]);die;
            }elseif($result['auth_code']){
                $url=Yii::$app->params['domain_url'].'/cloud/auth/checkAuth';
                $result=httpRequest($url,$data);
                $row=json_decode($result,true);
                if($row['code']==1&&$row['type']==1){
                    echo json_encode(['code'=>1,'msg'=>'网站已激活','type'=>1]);die;
                }else{
                    $authData=$row['authData'];
                    if($authData){
                        //var_dump($authData);die;
                        $row=ybwmEncrypt(json_encode($authData),Yii::$app->params['channel_model_name']);
                        touch('./web/secret.json');
                        chmod('./web/secret.json',0777);
                        if (!is_writable ('./web/secret.json')) {
                            echo json_encode(['code'=>2,'msg'=>'证书文件/web/secret.json无写入权限','type'=>2]);die;
                        }
                        file_put_contents('./web/secret.json',$row);
                        echo json_encode(['code'=>1,'msg'=>'网站已激活','type'=>1]);die;
                    }else{
                        //echo json_encode(['code'=>2,'msg'=>'站点授权失败,请联系管理员处理!','type'=>2]);die;
                    }
                }

            }else{
                $url=Yii::$app->params['domain_url'].'/cloud/auth/checkAuth';
                $result=httpRequest($url,$data);
                $row=json_decode($result,true);
                if($row['code']==1&&$row['type']==1){
                    echo json_encode(['code'=>1,'msg'=>'网站已激活','type'=>1]);die;
                }else{
                    $authData=$row['authData'];
                    if($authData){
                        //var_dump($authData);die;
                        $row=ybwmEncrypt(json_encode($authData),Yii::$app->params['channel_model_name']);
                        touch('./web/secret.json');
                        chmod('./web/secret.json',0777);
                        file_put_contents('./web/secret.json',$row);
                        echo json_encode(['code'=>1,'msg'=>'网站已激活','type'=>1]);die;
                    }else{
                        //echo json_encode(['code'=>1,'msg'=>'网站未激活','type'=>2]);die;
                    }
                }

            }


        }
    }

    //获取授权信息
    public function actionGetAuthInfo(){
        $request = Yii::$app->request;
        if ($request->isPost){
            $domainUrl=Yii::$app->request->hostInfo;
            $domainUrl= preg_replace("(^https?://)", "",$domainUrl);
            $fileData=file_get_contents('./web/secret.json');
            $jsonData=json_decode(ybwmDecrypt($fileData,Yii::$app->params['channel_model_name']),true);
            if($domainUrl=='bkycms.com' or $domainUrl=='v2.bkycms.com'){
                $domainUrl=$jsonData['domain_url'];
            }
            if($jsonData['authData']&&$jsonData['domain_url']==$domainUrl){
                $jsonData['ip_address']=gethostbyname($_SERVER['SERVER_NAME']);
                echo json_encode(['code'=>1,'msg'=>'成功','data'=>$jsonData]);die;
            }else{
                //echo json_encode(['code'=>2,'msg'=>'网站未激活或已停用']);die;
            }
        }
    }
    //网站更新授权信息
    public function actionAuthUp(){
        $domainUrl=Yii::$app->request->hostInfo;
        $data=array(
            'domain_url'=>$domainUrl,
            'ip'=>gethostbyname($_SERVER['SERVER_NAME']),
        );
        $url=Yii::$app->params['domain_url'].'/cloud/auth/authUp';
        $result=httpRequest($url,$data);
        $authData=json_decode($result,true)['authData'];
        //var_dump($result);die;
        if($authData){
            //var_dump($authData);die;
            $row=ybwmEncrypt(json_encode($authData),Yii::$app->params['channel_model_name']);
            touch('./web/secret.json');
            chmod('./web/secret.json',0777);
            file_put_contents('./web/secret.json',$row);
            echo json_encode(['code'=>1,'msg'=>'网站已更新授权','type'=>1]);die;
        }else{
            echo json_encode(['code'=>1,'msg'=>'网站授权更新失败','type'=>2]);die;
        }


    }


}